id/email
password
forgot password | create account
about | help | prefs
ReadingBatcode reading practice

 

 

For LoopsForLoop5

prev  |  next  |  chance

The .substring() method returns a portion of a string.

public static int loopIt(String str, String x, int y) {
    int result = 0;

    for (int i = y; i < str.length(); i++) {
        if (str.substring(i, i + 1).equals(x))
            result++;
    }

    return result;
}
Function Call  Return Value
loopIt("athenian", "e", 2)
loopIt("apples", "p", 1)
loopIt("hello", "a", 3)
loopIt("aa", "a", 1)

Experiment with this code on Gitpod.io

⬅ Back